package com.village.friend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.village.friend.dto.response.TokenResDto;
import com.village.friend.service.OpenImService;
import com.village.friend.utils.HttpUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class OpenImServiceImpl implements OpenImService {

    static String userTokenUrl = "http://appstone.top/imapi/auth/user_token";
    static String chatUrl = "http://appstone.top/imapi/manager/send_msg";
    static String secret = "tuoyun";

    private static String managerToken = null;

    static {
        Map map = new HashMap();
        map.put("secret", secret);
        map.put("platform", 8);
        map.put("uid", 1 + "");
        String s = HttpUtils.doPost(userTokenUrl, map, null);
        TokenResDto tokenResDto = JSONObject.parseObject(s, TokenResDto.class);
        managerToken = tokenResDto.getData().getToken();
    }

    @Override
    public String getToken(Integer uid) {
        Map map = new HashMap();
        map.put("secret", secret);
        map.put("platform", 2);
        map.put("uid", uid + "");
        String s = HttpUtils.doPost(userTokenUrl, map, null);
        System.out.println(s);
        TokenResDto tokenResDto = JSONObject.parseObject(s, TokenResDto.class);
        String token = tokenResDto.getData().getToken();
        return token;
    }


    @Override
    public void sendMsg(Integer sendID, String senderNickName, String senderFaceURL, Integer recvID, String content) {
        Map mapChat = new HashMap();
        mapChat.put("secret", "tuoyun");
        mapChat.put("OperationID", UUID.randomUUID().toString().replaceAll("-", ""));
        mapChat.put("sendID", sendID + "");
        mapChat.put("senderNickName", senderNickName);
        mapChat.put("senderFaceURL", senderFaceURL);
        mapChat.put("recvID", recvID + "");
        mapChat.put("contentType", 101);
        mapChat.put("sessionType", 1);
        Map mapContent = new HashMap();
        mapContent.put("text", content);
        mapChat.put("content", mapContent);
        HttpUtils.doPost(chatUrl, mapChat, managerToken);
    }

    @Override
    public void sendMsgByManager(Integer recvID, String content) {
        sendMsg(1, "官方消息", "https://g7-stone.oss-cn-guangzhou.aliyuncs.com/uploads/heads/manager.png", recvID, content);
    }

    @Override
    public void sendMsgByManagerLook(Integer recvID, String content) {
        sendMsg(2, "谁看过我", "", recvID, content);
    }

//    public static void main(String[] args) {
//        Map map = new HashMap();
//        map.put("secret", "tuoyun");
//        map.put("platform", 2);
//        map.put("uid", 77 + "");
//        String s = HttpUtils.doPost("http://appstone.top/imapi/auth/user_token", map, null);
//        System.out.println(s);
//        TokenResDto tokenResDto = JSONObject.parseObject(s, TokenResDto.class);
//        String token = tokenResDto.getData().getToken();
//    }

}
